package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.common.constant.RocketConstants;
import cn.itsource.ymcc.common.result.JsonResult;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "MediaConsumer",
        topic = RocketConstants.MEDIA_PUSH_VIDEO_TOPIC,
        selectorExpression = RocketConstants.MEDIA_PUSH_VIDEO_TAG
)
public class MediaConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService iMediaFileService;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String string = new String(body,"utf-8");
            MediaFile mediaFile = JSON.parseObject(string, MediaFile.class);
            JsonResult jsonResult = iMediaFileService.handleFile2m3u8(mediaFile);
            if(jsonResult.getSuccess()){
                System.out.println("异步推流完成");
            }else {
                //记录日志
                System.out.println("异步推流失败");
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
